﻿using Fast;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 添加Fast框架服务扩展类
    /// </summary>
    [SkipScan]
    public static class FastServiceCollectionExtensions
    {
        /// <summary>
        /// 服务注入基础配置（带Swagger）
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IFastApplication AddFast(this IServiceCollection services, Action<FastServiceOptions> configure = null)
        {
            Check.NotNull(services, nameof(services));

            var application = FastApplicationFactory.Create(services);

            ConfigureAspNetCoreServices(services, configure);

            return application;
        }

        /// <summary>
        /// 配置核心服务
        /// </summary>
        /// <param name="services"></param>
        private static void ConfigureAspNetCoreServices(IServiceCollection services, Action<FastServiceOptions> configure = null)
        {
            var options = new FastServiceOptions();
            configure?.Invoke(options);

            services.AddDynamicApiController(options.DynamicApiControllerConfigure);
            services.AddHttpContextAccessor();
            services.AddSwaggerDocument(options.SwaggerGenConfigure);
            services.AddFastException();
            services.AddConfigureOptions<AppSettingsOptions>();
        }
    }
}
